﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using IFTRUE.CMS.Models;
using MongoDB.Driver.Builders;
using IFTRUE.DataAdapter.MongoDB;
using System.Configuration;

namespace IFTRUE.CMS.Areas.Admin.Models
{
    #region 模型

    public class ChangePasswordModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "当前密码")]
        public string OldPassword { get; set; }

        [Required]
        [ValidatePasswordLength]
        [DataType(DataType.Password)]
        [Display(Name = "新密码")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "确认新密码")]
        [Compare("NewPassword", ErrorMessage = "新密码和确认密码不匹配。")]
        public string ConfirmPassword { get; set; }
    }

    public class LogOnModel
    {
        [Required]
        [Display(Name = "用户名")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "密码")]
        public string Password { get; set; }

        [Display(Name = "记住我?")]
        public bool RememberMe { get; set; }
    }

    public class UserModel
    {
        [Display(Name="帐号ID")]
        public string _id { get; set; }

        [Required]
        [Display(Name = "用户名")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "电子邮件地址")]
        public string Email { get; set; }

        [Required]
        [ValidatePasswordLength]
        [DataType(DataType.Password)]
        [Display(Name = "密码")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "确认密码")]
        [Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
        public string ConfirmPassword { get; set; }
    }
    #endregion

    #region 服务
    public class LogOnService
    {
        public bool LogOn(string username, string password)
        {
            var collection = MongoDBAdapter.GetDatabase(ConfigurationManager.ConnectionStrings["mongodb_conn"].ConnectionString, ConfigurationManager.AppSettings["cms_database"]).GetCollection("userinfo");
            var query = Query.EQ("UserName", username);
            var mongoDocument = collection.FindOne(query);

            if (mongoDocument != null && mongoDocument.GetElement("Password").Value.ToString() == password)
                return true;
            return false;
        }
    }

    public class UserService
    {
        public bool Save(UserModel model)
        {
            MongoDB.Driver.WriteConcernResult wcr = null;
            if (string.IsNullOrEmpty(model._id))
            {
                model._id = Guid.NewGuid().ToString();
                wcr = MongoDBAdapter.GetDatabase(ConfigurationManager.ConnectionStrings["mongodb_conn"].ConnectionString, ConfigurationManager.AppSettings["cms_database"]).GetCollection<UserModel>("userinfo").Insert(model);
            }
            else
                wcr = MongoDBAdapter.GetDatabase(ConfigurationManager.ConnectionStrings["mongodb_conn"].ConnectionString, ConfigurationManager.AppSettings["cms_database"]).GetCollection<UserModel>("userinfo").Save(model);
            return wcr.Ok;
        }
    }
    #endregion

    #region 验证

    #endregion
}